package com.lanyou.cook.entity.domain.userManagement;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.repository.userManagement.UserRepository;
import com.lanyou.cook.service.ApplicationContextKeeper;

@Entity
@EntityListeners({ AuditingEntityListener.class })
@Table(name = "T_COOK_DB_ROLE")
public class Role {
	
	private String id;	//角色ID ROLE_ID
	
	private String roleName;	//角色名称 ROLE_NAME
	
	private int version;	//并发控制字段    VERSION
	
	private String remark;	//备注  REMARK
	
	private String isEnable = "1";	//是否可用    IS_ENABLE
	
	private String creator;	//创建人    CREATOR
	
	private Date createTime;	//创建时间    CREATED_DATE
	
	private String modifier;	//最后更新人员    MODIFIER
	
	private Date modifyTime;	//最后更新时间    LAST_UPDATED_DATE

	private Set<Permission> permissions = new HashSet<>();
	
	private Set<User> user;
	
	public final static Specification<Role> EmptySpecification = new Specification<Role>(){
		@Override
		public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<Role> idSpecification(String id){
		return new Specification<Role>(){
			@Override
			public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "ROLE_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	
	@Column(name = "ROLE_NAME",nullable = false,unique = true, length = 36)
	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	@Version
	@JsonIgnore
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	@Column(name = "REMARK", length = 100)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	@Column(name = "IS_ENABLE", length = 100)
	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	@CreatedBy
	@Column(name = "CREATOR", length = 50, updatable=false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(name = "MODIFIER", length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable=false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	
	@ManyToMany
	@JoinTable(name = "T_COOK_BU_ROLE_PRI", 
		joinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID") , 
			inverseJoinColumns = @JoinColumn(name = "PRI_ID", referencedColumnName = "PRI_ID") 
	)
	public Set<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<Permission> permissions) {
		this.permissions = permissions;
	}

	@OneToMany(mappedBy="role")
	public Set<User> getUser() {
		return user;
	}

	public void setUser(Set<User> user) {
		this.user = user;
	}

	
	@Transient
	public String getUserCount() {
		UserRepository userRepository = ApplicationContextKeeper.getApplicationContext().getBean(UserRepository.class);
		try {
			return String.valueOf(userRepository.countByRole_Id(getId()));
			/*return String.valueOf(getUser().size());*/
		} catch (Exception e) {
			return null;
		}
	}
}
